package com.fan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fan.dao.ChapterDao;
import com.fan.dao.VideoDao;
import com.fan.entity.Chapter;
import com.fan.entity.Video;
import com.fan.service.ChapterService;
import com.fan.utils.CommonResults;
import com.sixth.until.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: 老范
 * @create: 2022-04-28 20:47
 **/
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterDao, Chapter> implements ChapterService {
    @Autowired
    private ChapterDao chapterDao;
    @Autowired
    private VideoDao videoDao;

    @Override
    public CommonResult findChapterByCourseId(String courseId) {
        List<Chapter> list = chapterDao.findChapterByCourseId(courseId);
        return new CommonResult(2000,"查询成功",list);
    }


    @Override
    public CommonResults findChapterById(String id) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        List<Chapter> chapters = chapterDao.selectList(wrapper);

//      获取课时
//        Video video1 = new Video();
        List<Video> videos = videoDao.selectList(null);

        List<Chapter> chapters1 = new ArrayList<>();
        for (Chapter chapter : chapters) {
            Chapter chapter1 = new Chapter();
            chapter1.setTitle(chapter.getTitle());
            chapter1.setSort(chapter.getSort());
            chapter1.setId(chapter.getId());
            List<Video> videos1 = new ArrayList<>();
            for (Video video : videos) {
                if (video.getChapterId().equals(chapter.getId())){
                    videos1.add(video);
                    chapter1.setVideo(videos1);
                }
            }
            chapters1.add(chapter1);
        }
        for (Chapter chapter2 : chapters1) {
            if (chapter2.getVideo()==null){
                chapter2.setPeriodNum(0);
            }else {
                chapter2.setPeriodNum(chapter2.getVideo().size());
            }
        }
        return new CommonResults(2000,"查询成功",chapters1);
    }


    @Override
    public CommonResults deleteChapterWithCourseId(Chapter chapter) {
        int i = chapterDao.deleteById(chapter.getId());
        if (i>0){
            CommonResults chapterById = findChapterById(chapter.getCourseId());
            List<Chapter> result = (List<Chapter>)chapterById.getResult();
            for (Chapter chapter1 : result) {
                if (chapter1.getSort()>chapter.getSort()){
                    chapter1.setSort(chapter1.getSort()-1);
                    int i1 = chapterDao.updateById(chapter1);
                    return new CommonResults(2000,"删除章节成功",null);
                }
            }
        }
        return new CommonResults(2000,"删除章节成功",null);
    }



    @Override
    public CommonResults insertChapter(Chapter chapter) {

        if (StringUtils.isEmpty(chapter.getId())){
            //为空则是插入
            Date date = new Date();
            chapter.setGmtCreate(date);
            chapter.setGmtModified(date);
            int insert = chapterDao.insert(chapter);
            if (insert>0){
                CommonResults commonResult = findChapterById(chapter.getCourseId());
                List<Chapter> result = (List<Chapter>) commonResult.getResult();
                return new CommonResults(2000,"新增章节成功",result);
            }else {
                return new CommonResults(5000,"新增章节失败",null);
            }
        }else {
            //修改该章名称
            Date date = new Date();
            chapter.setGmtModified(date);
            int i = chapterDao.updateById(chapter);
            if (i>0){
                CommonResults commonResult = findChapterById(chapter.getCourseId());
                List<Chapter> result = (List<Chapter>) commonResult.getResult();
                return new CommonResults(2000,"修改章节成功",result);
            }else {
                return new CommonResults(5000,"修改章节失败",null);
            }
        }
    }
}
